<template>
  <div class="m-page">
    <el-row :gutter="10">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="m-tree m-card">
          <div>
            <el-input
              v-model="deptName"
              placeholder="请输入部门名称"
              clearable
              size="small"
              prefix-icon="el-icon-search"
              style="margin-bottom: 20px"
            />
          </div>
          <div>
            <el-tree
              :data="deptOptions"
              :props="defaultProps"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"
              ref="tree"
              @node-click="handleNodeClick"
              highlight-current
              :default-expanded-keys="defaultCheckedKeys"
              node-key="deptId"
            />
          </div>
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <div class="m-card">
          <el-row :gutter="10">
            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
              >新增</el-button>
            </el-col>
          </el-row>

          <el-table
            v-loading="loading"
            class="m-margin-top"
            header-cell-class-name="m-table-header"
            stripe
            :data="videoMappingList">
            <el-table-column label="摄像机编号" align="center" prop="deviceNo" />
            <el-table-column label="摄像机位置" align="center" prop="orgName" />
            <el-table-column
              label="操作"
              align="center"
              width="160"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                >修改</el-button
                >
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </el-col>
    </el-row>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备编号" prop="deviceNo">
              <el-input v-model="form.deviceNo" placeholder="请输入设备编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组织机构" prop="orgId">
              <treeselect
                v-model="form.orgId"
                :options="deptOptions"
                :disable-branch-nodes="true"
                :normalizer="normalizer"
                :show-count="true"
                placeholder="请选择组织机构"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :loading="submitLoading"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Vue from 'vue'
import { getVideoMappingList, addVideoMapping, updateVideoMapping, detailVideoMapping, deleteVideoMapping } from '@/services/system/monitorMaintain'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { getDeptTreeDeptData } from '@/services/authorityManagement/laborPersonnel'

export default Vue.extend({
  name: 'monitorMaintain',
  components: { Treeselect },
  data () {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 监控维护数据
      videoMappingList: null,
      defaultCheckedKeys: [],
      // 弹出层标题
      title: '',
      submitLoading: false,
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 岗位选项
      postOptions: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: 'children',
        label: 'deptName'
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orgId: undefined,
        id: undefined,
        deviceNo: undefined
      },
      // 表单校验
      rules: {
        orgId: [
          { required: true, message: '归属部门不能为空', trigger: 'change' }
        ],
        deviceNo: [
          { required: true, message: '设备编号不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    // 根据名称筛选部门树
    deptName (val) {
      this.$refs.tree.filter(val)
    }
  },
  created () {
    this.getTreeselect()
  },
  methods: {
    /** 查询用户列表 */
    getList () {
      this.loading = true
      getVideoMappingList(this.queryParams).then((response) => {
        this.videoMappingList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 查询部门下拉树结构 */
    getTreeselect () {
      getDeptTreeDeptData().then((response) => {
        this.deptOptions = response.data
        console.log(this.deptOptions, '左侧部门树')
        this.$nextTick(() => {
          if (this.deptOptions.length > 0) {
            const newArr = []
            this.recursionFun(this.deptOptions, newArr)
          }
        })
      })
    },
    recursionFun (data, arr) {
      if (!data[0].children) {
        this.queryParams.orgId = data[0].deptId
        this.getList()
        this.defaultCheckedKeys = arr
        return this.$refs.tree.setCurrentKey(data[0].deptId)
      } else {
        arr.push(data[0].deptId)
        this.recursionFun(data[0].children, arr)
      }
    },
    normalizer (node) {
      if (!node.children) {
        delete node.children
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      }
    },
    // 筛选节点
    filterNode (value, data) {
      if (!value) return true
      return data.deptName.indexOf(value) !== -1
    },
    // 节点单击事件
    handleNodeClick (data) {
      this.queryParams.orgId = data.deptId
      this.getList()
    },

    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.form = {
        orgId: undefined
      }
      this.resetForm('form')
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset()
      this.open = true
      this.title = '添加监控'
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset()
      detailVideoMapping({ id: row.id }).then((res) => {
        this.form = res.data
        this.open = true
        this.title = '修改监控'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id !== undefined) {
            updateVideoMapping(this.form)
              .then(() => {
                this.$message.success('修改成功')
                this.open = false
                this.getList()
                this.submitLoading = false
              })
              .catch((err) => {
                this.$message.error(err.msg)
                this.submitLoading = false
              })
          } else {
            addVideoMapping(this.form)
              .then(() => {
                this.$message.success('新增成功')
                this.open = false
                this.getList()
                this.submitLoading = false
              })
              .catch(() => {
                this.submitLoading = false
              })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      this.$confirm('是否确认删除该数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function () {
          return deleteVideoMapping(row.id)
        })
        .then(() => {
          this.getList()
          this.$message.success('删除成功')
        })
        .catch((err) => {
          this.$message.error(err.msg)
        })
    }
  }
})
</script>
